import 'dart:io';
import 'dart:math';
import 'package:bwt_ad_flutter/BwtAdCore.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../BwtExpressAd.dart';

abstract class BwtAdWidget extends StatefulWidget {
  final String viewType;
  final BwtExpressAd bwtExpressAd;

  const BwtAdWidget({
    super.key,
    required this.viewType,
    required this.bwtExpressAd,
  });
}

class BwtAdWidgetState<T extends BwtAdWidget> extends State<T> {
  @override
  Widget build(BuildContext context) {
    var creationParams = {
      BwtAdCore.keyAdLoaderId: widget.bwtExpressAd.adLoaderId,
      BwtAdCore.keyAdId: widget.bwtExpressAd.adId,
      BwtAdCore.keyAdWidth: widget.bwtExpressAd.adWidth,
    };
    return widget.bwtExpressAd.adClosed
        ? SizedBox(width: widget.bwtExpressAd.adWidth, height: 0)
        : Container(
            width: widget.bwtExpressAd.adWidth,
            height: max(widget.bwtExpressAd.adHeight, 1),
            constraints: BoxConstraints(maxWidth: widget.bwtExpressAd.adWidth),
            child: Platform.isAndroid
                ? AndroidView(
                    viewType: widget.viewType,
                    creationParams: creationParams,
                    creationParamsCodec: const StandardMessageCodec(),
                  )
                : UiKitView(
                    viewType: widget.viewType,
                    creationParams: creationParams,
                    creationParamsCodec: const StandardMessageCodec(),
                  ),
          );
  }

  void update() {
    setState(() {});
  }
}
